package com.csun.cmny.provider.controller.message;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.entity.Page;
import com.csun.cmny.provider.model.domain.OmcFlowMessage;
import com.csun.cmny.provider.model.dto.message.FlowMessageQueryDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcFlowMessageService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-10-28 10:02
 * @Description:
 */
@RestController
@RequestMapping(value = "/flowMessage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcFlowMessageController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcFlowMessageService omcFlowMessageService;

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    public Wrapper add(@RequestBody OmcFlowMessage omcFlowMessage) {

        Integer result = omcFlowMessageService.add(omcFlowMessage, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.ok(result);
    }

    /**
     * 修改
     */
    @PostMapping(value = "/edit")
    public Wrapper edit(@RequestBody OmcFlowMessage omcFlowMessage) {

        Integer result = omcFlowMessageService.edit(omcFlowMessage, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.ok(result);
    }

    /**
     * 删除
     */
    @PostMapping(value = "/deleteById/{id}")
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = omcFlowMessageService.deleteById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.ok(result);
    }

    /**
     * 查询 - 分页
     */
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryListWithPage(@RequestBody FlowMessageQueryDto flowMessageQueryDto) {

        List<OmcFlowMessage> list = omcFlowMessageService.queryListWithPage(flowMessageQueryDto);

        return WrapMapper.ok(new PageInfo<>(list));
    }
}
